package org.fuys.coder.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.common.exception.handler.NeedCallBackException;
import org.fuys.coder.domain.substance.model.req.operation.SubstanceOperationReq;
import org.fuys.coder.domain.substance.repository.ISubstanceOperationRepository;
import org.fuys.coder.infrastructure.dao.FavoriteUserMapper;
import org.fuys.coder.infrastructure.dao.SubstanceFavoritesMapper;
import org.fuys.coder.infrastructure.dao.SubstanceReportMapper;
import org.fuys.coder.infrastructure.dao.SubstanceThumbMapper;
import org.fuys.coder.infrastructure.model.po.*;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.infrastructure.repository
 * @className: SubstanceOperationRepositoryImpl
 * @author: WangJing
 * @description: 仓储实现类
 * @date: 2024/7/13 12:07
 * @version: 1.0
 */
@Service
public class SubstanceOperationRepositoryImpl implements ISubstanceOperationRepository {

    @Resource
    private SubstanceThumbMapper substanceThumbMapper;

    @Resource
    private SubstanceFavoritesMapper substanceFavoritesMapper;

    @Resource
    private SubstanceReportMapper substanceReportMapper;

    @Resource
    private FavoriteUserMapper favoriteUserMapper;

    @Override
    public boolean thumbSubstance(Long substanceId, Integer userId) {
        try {
            SubstanceThumb substanceThumb = substanceThumbMapper.selectOne(new LambdaQueryWrapper<SubstanceThumb>()
                    .select(SubstanceThumb::getId).eq(SubstanceThumb::getSubstanceId, substanceId)
                    .eq(SubstanceThumb::getUserId, userId));
            if(ObjectUtils.isEmpty(substanceThumb)){
                substanceThumb=new SubstanceThumb();
                substanceThumb.setSubstanceId(substanceId);
                substanceThumb.setUserId(userId);
                substanceThumbMapper.insert(substanceThumb);
                return true;
            }
            substanceThumbMapper.deleteById(substanceThumb);
            return false;
        } catch (Exception e) {
            throw new NeedCallBackException(e.getMessage());
        }
    }

    @Override
    public boolean favoriteSubstance(Long substanceId,Long favId ,Integer userId) {
        try {
            //查询收藏夹和用户对应信息
            final FavoritesUser favoritesUser = favoriteUserMapper.selectOne(new LambdaQueryWrapper<FavoritesUser>()
                    .select(FavoritesUser::getId).eq(FavoritesUser::getUserId, userId).eq(FavoritesUser::getFavoritesId, favId));
            if(ObjectUtils.isEmpty(favoritesUser)){
                throw new BusinessException("收藏夹不属于你 请好好使用你的账号");
            }
            //查询记录
            SubstanceFavorites substanceFavorites = substanceFavoritesMapper.selectOne(new LambdaQueryWrapper<SubstanceFavorites>()
                    .select(SubstanceFavorites::getId).eq(SubstanceFavorites::getSubstanceId, substanceId)
                    .eq(SubstanceFavorites::getUserId, userId).eq(SubstanceFavorites::getFavId, favId));
            //如果记录为空 则新插入一条记录
            if(ObjectUtils.isEmpty(substanceFavorites)){
                substanceFavorites=new SubstanceFavorites();
                substanceFavorites.setSubstanceId(substanceId);
                substanceFavorites.setUserId(userId);
                substanceFavorites.setFavId(favId);
                substanceFavoritesMapper.insert(substanceFavorites);
                return true;
            }
            //否则是取消收藏
            substanceFavoritesMapper.deleteById(substanceFavorites);
            return false;
        } catch (Exception e) {
            throw new NeedCallBackException(e.getMessage());
        }
    }

    @Override
    public Long reportSubstance(SubstanceOperationReq req) {
        try {
            SubstanceReport substanceReport=new SubstanceReport();
            substanceReport.setSubstanceId(req.getSubstanceId());
            substanceReport.setUserId(req.getUserId());
            substanceReportMapper.insert(substanceReport);
            final Long count = substanceReportMapper.selectCount(new LambdaQueryWrapper<SubstanceReport>()
                    .eq(SubstanceReport::getSubstanceId, req.getSubstanceId()));
            return count;
        } catch (Exception e) {
            throw new NeedCallBackException(e.getMessage());
        }
    }
}
